package cn.feuler.user8080.service;

import cn.feuler.common.entity.User;
import cn.feuler.user8080.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Service
public class UserService {

    private UserMapper usermapper;

    @Autowired
    public UserService(UserMapper usermapper) {
        this.usermapper = usermapper;
    }

    public User register(String email, String password) {
        if (isRegistered(email)) {
            return null;
        }
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setCreateTime(LocalDateTime.now());
        usermapper.insert(user);
        return user;
    }

    public boolean isRegistered(String email) {
        return usermapper.selectOne(new QueryWrapper<User>().eq("email", email)) != null;
    }

    public User login(String email, String password) {
        return usermapper.selectOne(new QueryWrapper<User>().eq("email", email).eq("password", password));
    }
}
